package com.market.cash.ui.presenter

import com.market.cash.configs.CashRootConfigs
import com.market.cash.http.ApiService
import com.market.cash.http.ApiService.addCommonParams
import com.market.cash.http.IHttpCallback
import com.market.cash.pro.ProPresenter
import com.market.cash.ui.view.ICashRootPinStatusView
import com.market.cash.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/25 13:45
 * @Annotation：支付状态查询 Presenter 实现
 */
class CashRootPinStatusPresenter(private val iView: ICashRootPinStatusView) : ProPresenter(iView) {

    //三方订单id
    private var thirdOrderId = ""
    fun saveCashRootThirdOrderId(orderId: String){
        thirdOrderId = orderId
    }

    /**
     * 获取支付状态
     */
    fun loadCashRootPaymentStatusAction(isStartCountdown: Boolean = true){
        iView.showCashRootLoading()
        val params = JSONObject().addCommonParams().apply {
            put("path",ApiService.API_PATH.CASHROOT_API_LOAN_REPAYMENT_STATUS_PATH)
            put("product",iView.loadPName())
            put("token",iView.loadPToken())
            put("thirdOrderId",thirdOrderId)//三方订单id
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideCashRootLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    obj.put("isStartCountdown",isStartCountdown)
                    if (obj.has("transactionId")){
                        thirdOrderId = obj.optString("transactionId")
                    }

                    // PROCESSING /SUCCEEDED /FAILED
                    val status = obj.optString("status")
                    when(status){
                        "FAILED" -> {
                            iView.showCashRootPaymentFailed(obj)
                        }
                        "SUCCEEDED" -> {
                            iView.showCashRootPaymentSuccess(obj)
                        }
                        else -> {
                            iView.showCashRootPaymentProcess(obj)
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideCashRootLoading()
            }
        })
    }


    /**
     * 订单还款
     */
    fun loadCashRootOrderPayLinkAction(){
        iView.showCashRootLoading()
        val params = JSONObject().addCommonParams().apply {
            put("product",iView.loadPName())
            put("token",iView.loadPToken())
            put("mobile", SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_MOBILE))
            put("path",ApiService.API_PATH.CASHROOT_API_LOAN_REPAYMENT_URL_PATH)
        }
        if (CashRootConfigs.isUgx() || CashRootConfigs.isKsh()){//肯尼亚、乌干达 需要使用 getOrderRepayH5 获取链接
            params.put("path",ApiService.API_PATH.CASHROOT_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH)
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideCashRootLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    thirdOrderId = obj.optString("thirdOrderId")
                    loadCashRootPaymentStatusAction()
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

}